﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using ThuVien;
namespace TranTekBE.Maintenance
{
    public class func
    {
        public string getPhongBan(string idphongban)
        {
            DataSet listphongban = FunctionsUtils.Database2.ExecuteQuery("SELECT * FROM tbl_PhongBan WHERE ID=" + idphongban);
            if (listphongban != null)
            {
                foreach (DataRow Rows in listphongban.Tables[0].Rows)
                {
                    return Rows[1].ToString();
                }
            }
            return "Phòng ban không tồn tại";
        }
        public string getCapDo(string idcapdo)
        {
            DataSet caodo = FunctionsUtils.Database2.ExecuteQuery("SELECT * FROM tbl_CapDo WHERE ID=" + idcapdo);
            if (caodo != null)
            {
                foreach (DataRow Rows in caodo.Tables[0].Rows)
                {
                    return Rows[1].ToString();
                }
            }
            return "Cấp độ không tồn tại";
        }
        public string getTinhTrang(string idtinhtrang)
        {
            DataSet tinhtrang = FunctionsUtils.Database2.ExecuteQuery("SELECT * FROM tbl_TinhTrang WHERE ID=" + idtinhtrang);
            if (tinhtrang != null)
            {
                foreach (DataRow Rows in tinhtrang.Tables[0].Rows)
                {
                    return Rows[1].ToString();
                }
            }
            return "Tình trạng không tồn tại";
        }
        public string getCuaHang(string idcuahang)
        {
            DataSet tinhtrang = FunctionsUtils.Database.ExecuteQuery("SELECT C007001 as Name FROM T0070 WHERE C007000='" + idcuahang + "'");
            if (tinhtrang != null)
            {
                foreach (DataRow Rows in tinhtrang.Tables[0].Rows)
                {
                    return Rows[0].ToString();
                }
            }
            return "Cửa hàng không tồn tại";
        }
        public string getFullname(string iduser)
        {
            DataSet username = FunctionsUtils.Database.ExecuteQuery("SELECT C001904 as firstname, C001905 as lastname FROM T0019 WHERE C001901='" + iduser + "'");
            if (username != null)
            {
                foreach (DataRow Rows in username.Tables[0].Rows)
                {
                    return Rows[1].ToString() + " " + Rows[0].ToString();
                }
            }
            return "Tài khoản không tồn tại";
        }
        public string getGia(string idthuoc)
        {
            DataSet username = FunctionsUtils.Database.ExecuteQuery("select TOP 1 Sp.C001203 as 'MaSP' , Gia.C000704 as 'Gia' from T0012 SP  Left Join T0007 Gia on SP.C001200 = Gia.C000701 WHERE Sp.C001203 = '"+idthuoc+"' order by Gia.C000704 desc ");
            if (username != null && username.Tables[0].Rows.Count >0)
            {
                foreach (DataRow Rows in username.Tables[0].Rows)
                {
                    return Rows[1].ToString();
                }
            }
            return "0";
        }
        public int checkAdmin(string user)
        {
            DataSet cAdmin = FunctionsUtils.Database2.ExecuteQuery("SELECT * FROM tbl_Admin WHERE Username='" + user + "'");
            if (cAdmin != null)
            {
                foreach (DataRow Rows in cAdmin.Tables[0].Rows)
                {
                    return 1;
                }
            }
            return 0;
        }
        public string TextToHtml(string text)
        {
            text = HttpUtility.HtmlEncode(text);
            text = text.Replace("\r\n", "\r");
            text = text.Replace("\n", "\r");
            text = text.Replace("\r", "<br>\r\n");
            text = text.Replace("  ", " &nbsp;");
            return text;
        }
        public string getWhy (string id)
        {
            DataSet why = FunctionsUtils.Database2.ExecuteQuery("SELECT * FROM tbl_Why WHERE ID=" + id);
            string lydo = "Chưa xác định";
            if (why != null)
            {
                foreach (DataRow Rows in why.Tables[0].Rows)
                {
                    lydo = Rows[1].ToString();
                }
            }
            return lydo;
        }
    }
}